package com.seocoo.worker.order;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.seocoo.worker.R;
import com.seocoo.worker.base.menu.NavigationTextButton;
import com.seocoo.worker.base.menu.OnTabReselectListener;
import com.seocoo.worker.order.model.OrderModel;
import com.seocoo.worker.order.widget.NavTextFragment;
import com.seocoo.worker.user.login.PlatformDialog;
import com.seocoo.worker.user.model.UserModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMFragment;

/**
 * 功能描述:[星级工匠订单 主界面]-【安装订单，维修订单】
 *
 * @date 2020/2/16
 */
public class OrderHomeStarFragment extends MVVMFragment implements NavTextFragment.OnNavigationReselectListener {

    private NavTextFragment mNavBar;

    @Override
    public int getLayout() {
        return R.layout.order_home_star_fragment;
    }

    TextView tv_status;
    View gz;

    @Override
    public void initData(View view) {
        StatusBarUtil.setTransparentForImageViewInFragment(getActivity(), view.findViewById(R.id.fyContent));

        FragmentManager manager = getChildFragmentManager();
        mNavBar = ((NavTextFragment) manager.findFragmentById(R.id.homeFagNav));
        manager.beginTransaction().show(mNavBar).commitAllowingStateLoss();
        mNavBar.setup(getContext(), manager, R.id.homeContainer, this);

        tv_status = view.findViewById(R.id.tv_status);
        tv_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String selectTxt = tv_status.getText().toString();
                new StatusDialog(getActivity(), selectTxt, (dialog, item) -> {
                    dialog.dismiss();
                    //takeState:1-接单中 2-休息
                    if (TextUtils.equals("接单中", item)) {
                        getViewModel(OrderModel.class).changeTakeState("1");
                    } else if (TextUtils.equals("休息", item)) {
                        getViewModel(OrderModel.class).changeTakeState("2");
                    }

                }).show(v);
            }
        });
        gz = view.findViewById(R.id.gz);
        gz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getViewModel(UserModel.class).queryRule("1");
            }
        });
        getViewModel(OrderModel.class).queryTakeState();
        getViewModel(UserModel.class).queryRuleShow("1");
    }

    @LiveDataMatch
    public void showRuleIcon() {
        gz.setVisibility(View.VISIBLE);
    }

    @LiveDataMatch
    public void showRule(String msg) {
        PlatformDialog.showDialogUI(getActivity(), "抢单规则", msg);
    }

    @LiveDataMatch
    public void onChangeTakeState(String takeState) {
        if (TextUtils.equals("1", takeState)) {
            tv_status.setText("接单中");
        } else if (TextUtils.equals("2", takeState)) {
            tv_status.setText("休息");
        }
    }

    @Override
    public void onReselect(NavigationTextButton navigationButton) {
        Fragment fragment = navigationButton.getFragment();
        if (fragment instanceof OnTabReselectListener) {
            OnTabReselectListener listener = (OnTabReselectListener) fragment;
            listener.onTabReselect();
        }
    }
}
